<?php

namespace App\Http\Controllers;

use App\Models\ZZMed\ZHospMember;
use App\Traits\SignHelper;
use Illuminate\Http\Request;
use \Illuminate\Support\Facades\Validator;
use App\Traits\ResponseFormat;

class PasswordController extends Controller
{
    use SignHelper, ResponseFormat;

    public function __construct()
    {
        
    }

    /**
     *  找回密码 发送验证码
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
     */
    public function getVerificationCode(Request $request)
    {
        $rules = [
            'mobile' => [
                'required',
                'regex:$1(\d){10}$'
            ]
        ];

        if (Validator::make($request->input(), $rules)->fails()) {
            return $this->error(40007, "参数错误");
        }

        if (!$accounts = ZHospMember::query()->where('cell', $request->input('mobile'))->first()) {
            return $this->error(40015, '手机号未注册，找不到对应账号信息');
        }

        $this->sendSms($request->input('mobile'), 4);

        return $this->success([]);
    }

    /**
     *  验证验证码并返回登录用户信息
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
     */
    public function verify(Request $request)
    {
        $rules = [
            'mobile' => [
                'required',
                'regex:$1(\d){10}$'
            ],
            'verify_code' => 'required',
        ];

        $messages = [
            'mobile.required' => '手机号码不能为空',
            'mobile.regex' => '无效的手机号码',
            'verify_code.required' => '验证码不能为空',
        ];

        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            $errors = array_values($validator->errors()->toArray());
            return $this->error(42200, $errors[0][0]);
        }

        $validation = $this->verifySmsCode($request->input('mobile'), $request->input('verify_code'), 4);

        if (!$validation[0]) {
            return $this->error(40012, $validation[1] ?? '');
        }

        if ($accounts = ZHospMember::query()->where('cell', $request->input('mobile'))->first()) {
            return $this->success(['mobile' => $request->input('mobile'), 'username' => $accounts->login_id]);
        }

        return $this->error(40015, '手机号未注册，找不到对应账号信息');
    }


    /**
     *  验证验证码并重置密码
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
     */
    public function reset(Request $request)
    {
        $rules = [
            'mobile' => [
                'required',
                'regex:$1(\d){10}$'
            ],
//            'username' => 'required',
            'password' => 'required',
            'confirm_password' => 'required|same:password',
            'verify_code' => 'required'
        ];

        $messages = [
            'mobile.required' => '手机号码不能为空',
            'mobile.regex' => '无效的手机号码',
//            'username.required' => '用户名不能为空',
            'password.required' => '密码不能为空',
            'confirm_password.required' => '确认密码不能为空',
            'same' => '两次密码输入不一致',
            'verify_code.required' => '短信验证码不能为空'
        ];

        $params = $request->all();
        $validators = Validator::make($params, $rules, $messages);

        if ($validators->fails()) {
            $errors = array_values($validators->errors()->toArray());
            return $this->error(42200, $errors[0][0]);
        }

        if (!ZHospMember::query()->where('cell', $params['mobile'])
//            ->where('login_id', $request->input('username'))
            ->first()) {
            return $this->error(40015, '手机号和用户名不匹配，请核对后重试');
        }

        $validation = $this->verifySmsCode($request->input('mobile'), $request->input('verify_code'), 4);

        if (!$validation[0]) {
            return $this->error(40012, $validation[1] ?? '');
        }

        if (ZHospMember::resetPassword($params['username'] ?? '', $params['mobile'], $params['password'])) {
            return $this->success([]);
        }

        return $this->error(4003,'非法请求');
    }


}
